/**
 * 项目名称: Topic_654.cpp
 * 项目路径: study/DataStructure/LeetCode/树/中等
 * 编辑时间: 2024年09月01日16时34分21秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    TreeNode* construct(const vector<int>& nums,int left,int right){
        if(left>right)return nullptr;
        int best = left;
        for(int i = left + 1;i<=right;++i)
            if(nums[i]>nums[best])best=i;
        TreeNode* node = new TreeNode(nums[best]);
        node->left = construct(nums,left,best-1);
        node->right = construct(nums,best+1,right);
        return node;
    }
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
      return construct(nums,0,nums.size()-1);
    }
};